package com.itsccn.springaop.exception;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;

/** 
* @ClassName: DefaultException 
* @Description: TODO(统一异常处理) 
* @author 孙晨
* @date 2016年12月16日 下午5:32:59 
*  
*/
@Component("exceptionHandler")
public class DefaultException implements HandlerExceptionResolver {
	
	Logger log = Logger.getLogger(getClass());
	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,Exception ex) {
		ModelAndView mv = new ModelAndView();
		FastJsonJsonView view = new FastJsonJsonView();
		Map<String, Object> attributes = new HashMap<String, Object>();
		attributes.put("code", "1000001");
		attributes.put("msg", ex.getMessage());
		view.setAttributesMap(attributes);
		mv.setView(view);
		log.error("异常:" + ex.getMessage(), ex);
		return mv;
	}

}
